


layui.define(["jquery"], function (exports) {
    var $ = layui.jquery;
    var obj = {
        update(){
            console.log('update2');
        },
        post(option){
            return this.onRequest(option,'POST')
        },
        postJson(option){
            option.data=JSON.stringify(option.data)
            return this.onRequest(option,'POST','application/json')
        },
        get(option){
        return this.onRequest(option,'GET')
        },
        getUrl(url){
            return '/index.php/'  + url;
        },
        onRequest(option,type,contentType='application/x-www-form-urlencoded; charset=UTF-8'){
            let url=this.getUrl(option.url);
            console.log(url);
            return new Promise((resolve,reject)=>{
                $.ajax({
                    url,
                    // async:false,
                    type: type,
                    contentType: contentType,
                    dataType: "json",
                    data: option.data,
                    timeout: 60000,
                    success: function (res) {
                       if(res.code==1){
                           if(option.toast&&!option.error_toast){
                               layer.msg(res.msg, {icon: 1, shade:  [0.02, '#000'], scrollbar: false, time: 2000, shadeClose: true}, function () {
                           resolve(res.data);
                               })
                           }else{
                           resolve(res.data);
                           }
                       }else{
                           if(option.toast){
                               layer.msg(res.msg, {icon: 2, shade:  [0.02, '#000'], scrollbar: false, time: 3000, shadeClose: true}, function () {

                               })
                           }
                           reject(res.msg);
                       }
                    },
                    error: function (xhr, textstatus, thrown) {
                        reject('网络异常');
                     console.log(xhr);
                        return false;
                    }
                });
            })
        }
    };
    exports('http', obj);
});
